package com.kfm.ajax.controller;

import com.kfm.ajax.model.Result;
import com.kfm.ajax.model.Student;
import com.kfm.ajax.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin("*")
public class StudentController {

    @Resource
    private StudentService service;

    @GetMapping("/list")
    public Result list() {
        List<Student> all = service.findAll();
        return all == null ? Result.failure("查询失败") : Result.success("查询成功", all);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        int i = service.deleteByPrimaryKey(id);
        return i > 0 ? Result.success("删除成功") : Result.failure("删除失败");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Student student) {
        int i = service.updateByPrimaryKey(student);
        return i > 0 ? Result.success("修改成功") : Result.failure("修改失败");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Student student) {
        int i = service.insertSelective(student);
        return i > 0 ? Result.success("添加成功") : Result.failure("添加失败");
    }
}
